package cn.com.itg.common;

import cn.com.itg.pojo.domain.entity.MATDOC;
import cn.com.itg.pojo.domain.entity.VBFA;
import cn.com.itg.pojo.domain.entity.ZMSEG;
import cn.com.itg.pojo.domain.entity.ZTTCIDENTICAL;
import cn.com.itg.pojo.domain.vo.MatdocSendVO;
import cn.com.itg.pojo.domain.vo.OthInSingleSaveVO;
import cn.com.itg.pojo.domain.vo.OthoutSingleSaveVO;
import cn.com.itg.pojo.domain.vo.SalesOutMergeSourceDataSaveVO;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

public class BuildVOUtil implements Serializable {

    public static MatdocSendVO buildSendVOByZqrbwart(MATDOC matdoc, ZTTCIDENTICAL zttcidentical) {
        MatdocSendVO matdocSendVO = new MatdocSendVO();
        matdocSendVO
                .setGuid(
                        new StringBuffer()
                                .append(Optional.ofNullable(matdoc.getKey1()).orElse(""))
                                .append(Optional.ofNullable(matdoc.getKey2()).orElse(""))
                                .append(Optional.ofNullable(matdoc.getKey3()).orElse(""))
                                .append(Optional.ofNullable(matdoc.getKey4()).orElse(""))
                                .append(Optional.ofNullable(matdoc.getKey5()).orElse(""))
                                .append(Optional.ofNullable(matdoc.getKey6()).orElse(""))
                                .toString()
                )
                .setZrefflag(zttcidentical.getZres2())
                .setZritype(zttcidentical.getZfield2())
                .setZmblnr(
                        new StringBuffer()
                                .append(Optional.ofNullable(matdoc.getMblnr()).orElse(""))
                                .append(Optional.ofNullable(matdoc.getMjahr()).orElse(""))
                                .toString()
                )
                .setWerks(matdoc.getWerks())
                .setBldat(matdoc.getBldat())
                .setBwart(zttcidentical.getZfield2())
//                .setBwart(matdoc.getBwart())
                .setLgort(matdoc.getLgort())
//                .setUsnam(matdoc.getUsnam())
                .setBktxt(matdoc.getBktxt())
                .setZeile(matdoc.getZeile())
                .setCharg(matdoc.getCharg())
                .setMeins(matdoc.getMeins())
                .setSgtxt(matdoc.getSgtxt())
                .setZrefdoc(matdoc.getAblad())
                .setZrefdoc1(matdoc.getEmatn())
                //用于sgi
                .setHdriver(matdoc.getServiceperformer())
                .setCarno(matdoc.getOicTruckn())
                //固定值
                .setUpdateflag("Insert")
                .setZbatchflag(true)

                //其他计算使用中间数据
//                .setZres2(zttcidentical.getZres2())
                .setMatnr(matdoc.getMatnr())
                .setMblnr(matdoc.getMblnr())
                .setMjahr(matdoc.getMjahr())
                .setVbelnIm(matdoc.getVbelnIm())
                .setVbelpIm(matdoc.getVbelpIm())
                .setEbeln(matdoc.getEbeln())
                .setEbelp(matdoc.getEbelp())
                .setTimestamp(matdoc.getTimestamp())


        ;
        //预留字段
//                .setZzfield1()
//                .setZzfield2()
//                .setZzfield3()
//                .setZzfield4()
//                .setZzfield5()
//                .setZzfield6()
//                .setZzfield7()
//                .setZzfield8()
//                .setZzfield9()
//                .setZzfield0()

        BigDecimal cv = new BigDecimal(-1);
        if (Objects.equals(zttcidentical.getZres1(), "+")) {
            cv = new BigDecimal(1);
        }
        matdocSendVO.setZmenge(Optional.ofNullable(matdoc.getMenge()).orElse(new BigDecimal(0)).multiply(cv));
        matdocSendVO
                .setZzsenmeins1(matdoc.getMeins())//吨
                .setZsenmenge1(Optional.ofNullable(matdoc.getMenge()).orElse(new BigDecimal(0)).multiply(cv))
                .setZconvtype("0");
        matdocSendVO.setZconvrate(BigDecimal.valueOf(1));
        return matdocSendVO;
    }


    public static MatdocSendVO buildSendVOByZmseg(MatdocSendVO matdocSendVO, ZMSEG zmseg) {
        matdocSendVO
            //这里是ZMSEG表
                .setHdriver(zmseg.getHdriver())
                .setCarno(zmseg.getCarno())
        ;
        return matdocSendVO;
    }

    public static MatdocSendVO buildSendVOByVbfa(MatdocSendVO matdocSendVO, VBFA vbfa) {

        matdocSendVO
                .setZrefdoc(StringUtils.isEmpty(vbfa.getVbelv())?matdocSendVO.getEbeln():vbfa.getVbelv())
                .setZrefdoc1(StringUtils.isEmpty(vbfa.getPosnv())?matdocSendVO.getEbelp():vbfa.getPosnv());
        return matdocSendVO;
    }


    public static OthInSingleSaveVO buildOthInSingleSaveVO(MatdocSendVO matdocSendVO) {
        OthInSingleSaveVO result = new OthInSingleSaveVO();
        result.setData(new OthInSingleSaveVO.DataBean());
        result.getData()
                .setResubmitCheckKey(UUID.randomUUID().toString().replace("-",""))
//                .setResubmitCheckKey(matdocSendVO.getGuid())
                .setCode(matdocSendVO.getZmblnr())
                .setOrg(matdocSendVO.getWerks())
                .setAccountOrg(matdocSendVO.getWerks())
                .setVouchdate(matdocSendVO.getBldat())
                .setBustype(matdocSendVO.getBwart())
                .setWarehouse(matdocSendVO.getLgort())
                .setStockMgr(matdocSendVO.getUsnam())
                .setMemo(matdocSendVO.getBktxt())
                .set_status(matdocSendVO.getUpdateflag());
                //更新时才需要传
//                .setId(matdocSendVO.getGuid());

        //无映射关系的
//                .setNeedCalcLines()
//                .setStore(matdocSendVO.)
//                .setDepartment(matdocSendVO.)
//                .setOperator(matdocSendVO.)
//                .setHeadItem(matdocSendVO.)
//                .setDefines(matdocSendVO.)
        //类型不一致

        OthInSingleSaveVO.DataBean.OthInRecordsBean othInRecords = new OthInSingleSaveVO.DataBean.OthInRecordsBean();
        othInRecords
                .setProduct(matdocSendVO.getZmatnr())
                .setProductsku(matdocSendVO.getZmatnr())
                .setFree1(matdocSendVO.getMaktx())
                .setBatchno(matdocSendVO.getCharg())
                .setQty(matdocSendVO.getZmenge())
                .setUnit(matdocSendVO.getMeins())
                .setSubQty(matdocSendVO.getZsenmenge1())
                .setInvExchRate(matdocSendVO.getZconvrate())
                .setStockUnitId(matdocSendVO.getZzsenmeins1())
                .setUnitExchangeType(new Integer(Optional.ofNullable(matdocSendVO.getZconvtype()).orElse("1")))
                .setIsBatchManage(matdocSendVO.getZbatchflag())
                .setMemo(matdocSendVO.getSgtxt())
                .set_status(matdocSendVO.getUpdateflag())
                .setId(matdocSendVO.getZeile())
                .setRowno(matdocSendVO.getZeile())
//                .setBodyItem(new DefinesVO()
//                                .setId(matdocSendVO.getZeile())
//                                .setDefine1(matdocSendVO.getZrefdoc())
////                                    .setDefine2()
//                )
                ;

        //类型不一致

        //无映射
//                .setIsSerialNoManage()
//                .setFree2(matdocSendVO)
//                .setProducedate(matdocSendVO)
//                .setInvaliddate(matdocSendVO)
//                .setDefine1(matdocSendVO)
//                .setDefine2(matdocSendVO)
//                .setContactsQuantity(matdocSendVO)
//                .setContactsPieces(matdocSendVO)
//                .setNatUnitPrice(matdocSendVO)
//                .setNatMoney(matdocSendVO)
//                .setGoodsposition(matdocSendVO)
//                .setProject(matdocSendVO)
//                .setAutoCalcCost(matdocSendVO)
//                .setIsExpiryDateManage(matdocSendVO)

        result.getData()
                .setOthInRecords(new ArrayList<OthInSingleSaveVO.DataBean.OthInRecordsBean>() {
                    {
                        add(othInRecords);
                    }
                });
//        String resultJson = JSON.toJSONString(result, SerializerFeature.WriteNullStringAsEmpty);
//        return JSON.parseObject(resultJson,OthInSingleSaveVO.class);
        return result;
    }

    public static OthoutSingleSaveVO buildOthoutSingleSaveVO(MatdocSendVO matdocSendVO) {
        OthoutSingleSaveVO result = new OthoutSingleSaveVO();
        result.setData(new OthoutSingleSaveVO.DataBean());
        result.getData()
                .setResubmitCheckKey(UUID.randomUUID().toString().replace("-",""))
                .setCode(matdocSendVO.getZmblnr())
                .setOrg(matdocSendVO.getWerks())
                .setAccountOrg(matdocSendVO.getWerks())
                .setVouchdate(matdocSendVO.getBldat())
                .setBustype(matdocSendVO.getBwart())
                .setWarehouse(matdocSendVO.getLgort())
                .setStockMgr(matdocSendVO.getUsnam())
                .setMemo(matdocSendVO.getBktxt())
                .set_status(matdocSendVO.getUpdateflag())
                //TODO:确认getZrefdocN还是P
                .setSrcBillType(matdocSendVO.getZrefdoc())//这里需要确认
        //更新时才需要传
//                .setId(matdocSendVO.getGuid())
        ;

        //无映射关系的
//                .setNeedCalcLines()
//                .setStore(matdocSendVO.)
//                .setDepartment(matdocSendVO.)
//                .setOperator(matdocSendVO.)
//                .setHeadItem(matdocSendVO.)
//                .setDefines(matdocSendVO.)
        //类型不一致

        OthoutSingleSaveVO.DataBean.OthOutRecordsBean othOutRecords = new OthoutSingleSaveVO.DataBean.OthOutRecordsBean();
        othOutRecords
                .setId(matdocSendVO.getZeile())
                .setProduct(matdocSendVO.getZmatnr())
                .setProductsku(matdocSendVO.getZmatnr())
                .setBatchno(matdocSendVO.getCharg())
                .setQty(matdocSendVO.getZmenge())
                .setUnit(matdocSendVO.getMeins())
                .setSubQty(matdocSendVO.getZsenmenge1())
                .setInvExchRate(matdocSendVO.getZconvrate())
                .setStockUnitId(matdocSendVO.getZzsenmeins1())
                .setMemo(matdocSendVO.getSgtxt())
                .set_status(matdocSendVO.getUpdateflag())
                //TODO:需要核对
                .setUpcode(matdocSendVO.getZrefdoc())
                //更新类型才需要传
//                .setSourceid(matdocSendVO.getZrefdoc())
        ;

        //类型不一致
//                .setRowno(matdocSendVO.getZeile())

        //无映射
//                .setIsSerialNoManage()
//                .setFree2(matdocSendVO)
//                .setProducedate(matdocSendVO)
//                .setInvaliddate(matdocSendVO)
//                .setDefine1(matdocSendVO)
//                .setDefine2(matdocSendVO)
//                .setContactsQuantity(matdocSendVO)
//                .setContactsPieces(matdocSendVO)
//                .setNatUnitPrice(matdocSendVO)
//                .setNatMoney(matdocSendVO)
//                .setGoodsposition(matdocSendVO)
//                .setProject(matdocSendVO)
//                .setAutoCalcCost(matdocSendVO)
//                .setIsExpiryDateManage(matdocSendVO)
//                .setUnitExchangeType()
//                .setIsBatchManage(matdocSendVO.getZbatchflag())
//                .setBodyItem();

        result.getData()
                .setOthOutRecords(new ArrayList<OthoutSingleSaveVO.DataBean.OthOutRecordsBean>() {
                    {
                        add(othOutRecords);
                    }
                });
//        String resultJson = JSON.toJSONString(result, SerializerFeature.WriteNullStringAsEmpty);
//        return JSON.parseObject(resultJson,OthoutSingleSaveVO.class);
        return result;
    }

    public static SalesOutMergeSourceDataSaveVO buildSalesOutMergeSourceDataSaveVO(MatdocSendVO matdocSendVO) {
        SalesOutMergeSourceDataSaveVO result = new SalesOutMergeSourceDataSaveVO();
        result.setData(new SalesOutMergeSourceDataSaveVO.DataBean());
        result.getData()
                .setMergeSourceData(matdocSendVO.getZbatchflag())
                .setVouchdate(matdocSendVO.getBldat())
                .setWarehouse(matdocSendVO.getLgort())
                .setCode(matdocSendVO.getZmblnr())
                .setCLogisticsUserName(matdocSendVO.getHdriver())
//                .setCLogisticsUserPhone()
                .setCLogisticsCarNum(matdocSendVO.getCarno())
                .setBustype(matdocSendVO.getBwart())
                .set_status(matdocSendVO.getUpdateflag())
        ;
        SalesOutMergeSourceDataSaveVO.DataBean.DetailsBean details = new SalesOutMergeSourceDataSaveVO.DataBean.DetailsBean();
        details
                .set_status(matdocSendVO.getUpdateflag())
                .setQty(matdocSendVO.getZmenge())
                .setBatchno(matdocSendVO.getCharg())
                .setMakeRuleCode(matdocSendVO.getZrefflag())
                .setSourceid(matdocSendVO.getZrefdoc())
                .setSourceautoid(matdocSendVO.getZrefdoc1())
        //缺少映射
//                .setGoodsposition()
//                .setSalesOutsSNs()
        ;
        result.getData()
                .setDetails(new ArrayList<SalesOutMergeSourceDataSaveVO.DataBean.DetailsBean>() {
                    {
                        add(details);
                    }
                });
//        String resultJson = JSON.toJSONString(result, SerializerFeature.WriteNullStringAsEmpty);
//        return JSON.parseObject(resultJson,SalesOutMergeSourceDataSaveVO.class);
        return result;
    }
}
